package com.sdkj.controller.customer;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.entity.User;
import com.sdkj.service.customer.CustomerService;
import com.sdkj.util.AppUtil;
import com.sdkj.util.Const;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.PageData;
import com.sdkj.util.Tools;

/** 
 * 类名称：CustomerController
 * 创建人：王芳芳 
 * 创建时间：2017年5月11日
 * @version
 */
@Controller
@RequestMapping(value="/customer")
public class CustomerController extends BaseController{

	String menuUrl = "customer/listCustomers.do"; //菜单地址(权限用)
	
	@Resource(name="customerService")
	private CustomerService customerService;
	
	

	
	
	
	/**
	 * 保存用户
	 */
	@RequestMapping(value="/save")
	public ModelAndView save(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Jurisdiction.buttonJurisdiction(menuUrl, "add")){
			User user=getCurrentUser();
			pd.put("customer_company_id", this.get32UUID());	//ID
			pd.put("user_id",user.getUSER_ID());//创建人ID
			pd.put("company_id",user.getCompanyId());//创建人公司ID
			pd.put("department_id",user.getDepartmentId());//创建人部门ID
			pd.put("creat_time", Tools.date2Str(new Date()));				//最后登录时间
			pd.put("update_time",Tools.date2Str(new Date()));						//IP
			if(null == customerService.findById(pd)){
				customerService.save(pd);
				mv.addObject("msg","success");
			}else{
				mv.addObject("msg","failed");
			}
		} //判断新增权限
		
		mv.setViewName("save_result");
		return mv;
	}
	
	
	
	/**
	 * 保存用户
	 */
	@RequestMapping(value="saveUser")
	public ModelAndView saveUser(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Jurisdiction.buttonJurisdiction(menuUrl, "add")){
			pd.put("customer_id", this.get32UUID());	//ID
			pd.put("creat_time", Tools.date2Str(new Date()));	
				customerService.saveUser(pd);
				mv.addObject("msg","success");
		} 
		mv.setViewName("save_result");
		return mv;
	}
	
	
	
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增Customer页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("customer/company/customer_add");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	
	/**
	 * 去新增人员页面
	 */
	@RequestMapping(value="/goAddUser")
	public ModelAndView goAddUser(){
		logBefore(logger, "去新增Customer页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = customerService.findById(pd);	//根据ID读取
			mv.setViewName("customer/userInfo/userInfo_add");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改customer页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = customerService.findById(pd);	//根据ID读取
			mv.setViewName("customer/company/customer_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	
	/**
	 * 修改用户
	 */
	@RequestMapping(value="/edit")
	public ModelAndView editU() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){
			customerService.edit(pd);
		}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 删除用户
	 */
	@RequestMapping(value="/delete")
	public void deleteU(PrintWriter out){
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){
				customerService.delete(pd);
				}
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	
	
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		PageData pd = new PageData();
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayUSER_IDS[] = DATA_IDS.split(",");
				if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){
					customerService.deleteAll(ArrayUSER_IDS);
				}
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	
	/**
	 * 显示用户列表(用户组)
	 */
	@RequestMapping(value="/listCustomers")
	public ModelAndView listUsers(Page page)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		String USERNAME = pd.getString("USERNAME");
		
		if(null != USERNAME && !"".equals(USERNAME)){
			USERNAME = USERNAME.trim();
			pd.put("USERNAME", USERNAME);
		}
		
		String lastLoginStart = pd.getString("lastLoginStart");
		String lastLoginEnd = pd.getString("lastLoginEnd");
		
		if(lastLoginStart != null && !"".equals(lastLoginStart)){
			lastLoginStart = lastLoginStart+" 00:00:00";
			pd.put("lastLoginStart", lastLoginStart);
		}
		if(lastLoginEnd != null && !"".equals(lastLoginEnd)){
			lastLoginEnd = lastLoginEnd+" 00:00:00";
			pd.put("lastLoginEnd", lastLoginEnd);
		} 
		page.setPd(pd);
		List<PageData>	varList = customerService.listPdPageCustomer(page);			//列出用户列表
		mv.setViewName("customer/company/customer_list");
		mv.addObject("varList", varList);
		mv.addObject("pd", pd);
		mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		return mv;
	}

	/* ===============================权限================================== */
	@SuppressWarnings("unchecked")
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */

}
